package Command;

import Server.Command;
import Server.cmdArguments;
import Command.HistoryTracker;

public class UndoCommand implements Command {
	private cmdArguments args;

	public UndoCommand() {}

	@Override
	public String execute() {
		String result = "Undo is not possible when you have the database.";
		if (!args.db) {
			HistoryTracker ht = HistoryTracker.getInstance();
			ht.pollLastCommand(); //removing self
			Command cmd = ht.pollLastCommand();
			
			if (cmd != null) {
				ht.addUndo(cmd); //move to undone commands list
				result = cmd.undo();
			} else {
				result = "Nothing to undo.";
			}
		}

		return result;
	}

	@Override 
	public String undo() {
		return "Undo is not possible on an undo, try redo.";
	}

	@Override
	public void setArgs(cmdArguments args) {
		this.args = args;
	}

}